/**
 * Copyright (C) 2009 Noel Ang
 *
 * This file is part of Samsara.
 *
 * Samsara is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Samsara is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Samsara.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package com.lunarshades.samsara.ui.actions;

import java.util.LinkedHashSet;
import java.util.Set;

import com.lunarshades.samsara.Raider;
import com.lunarshades.samsara.ui.Utils;

/**
 * A ContestWinEvent describes a resolved karma contention event.
 *
 * @author Noel Ang, sparta.variabl@gmail.com
 */
public class ContestWinEvent<T extends Raider> extends KarmaSubtractEvent<T> {

/**
 * Create a ContestWinEvent.
 *
 * @param subject The winner.
 * @param others  The set of losers.
 * @param amount  Karma cost for the winner. Must be a zero or non-negative
 *                value.
 * @param context Additional info describing the context under which the event
 *                is taking place. (Optional)
 *
 * @throws NullPointerException     if subject or others is null.
 * @throws IllegalArgumentException if amount is a negative value.
 */
public ContestWinEvent(T subject, Set<T> others, int amount, String context) {
    super(subject, amount, context);
    mOthers.addAll(others);
    mIsContested = !mOthers.isEmpty();
}

@Override
protected String initMessage() {
    // WINNER [Person] karma [2] (12 @> 10) (for [Epic Loot of Shit])? vs [Person1, ...]
    StringBuffer buffer = new StringBuffer();

    buffer.append("WINNER ");

    // person
    buffer.append("[").append(subject().name()).append("] ");

    // karma amount
    buffer.append("karma [");
    buffer.append(magnitude()).append("] ");

    // total karma
    buffer.append("(");
    try {
        int newKarma = Utils.makeInt(subject().karma().toString());
        int oldKarma = newKarma + magnitude();
        buffer.append(oldKarma)
                .append(mIsContested ? " > " : " @> ")
                .append(newKarma);
    } catch (NumberFormatException e) {
        buffer.append("ERR: ").append(subject().karma().toString());
    } finally {
        buffer.append(")");
    }

    // context
    if (!"".equals(context())) {
        buffer.append(" for [").append(context()).append("]");
    }

    // losers
    if (mIsContested) {
        buffer.append(" vs [");
        for (T other : mOthers) {
            buffer.append(other.name()).append(" ");
        }
        int end;
        buffer.replace((end = buffer.length()) - 1, end, "]");
    }

    return buffer.toString();
}

public boolean isContested() {
    return mIsContested;
}

private final Set<T> mOthers = new LinkedHashSet<T>();
private final boolean mIsContested;
}
